package com.example.monit.service;

import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
import co.elastic.clients.elasticsearch._types.aggregations.Buckets;
import co.elastic.clients.elasticsearch._types.aggregations.HistogramBucket;
import com.example.monit.dto.TotalDto;
import com.example.monit.utils.TimeUtils;
import com.example.monit.vo.BasTotalVo;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * es 共享操作
 */
public class BasEsService {

    protected List<TotalDto> totalData(Buckets<HistogramBucket> bucket, BasTotalVo basTotalVo) {
        List<HistogramBucket> list = bucket.array();
        List<TotalDto> linkedList = new LinkedList<>();

        // 间隔单位
        int intValue = Integer.parseInt(basTotalVo.getGranularity().split("[smhdMy]")[0]);
        String type = basTotalVo.getGranularity().split(intValue + "")[1];
        ChronoUnit unit = TimeUtils.getType(type);

        LocalDateTime startTime = basTotalVo.getStart_time();
        LocalDateTime endTime = basTotalVo.getEnd_time();

        // 假如没有数据
        if (ObjectUtils.isEmpty(list)) {
            while (startTime.isBefore(endTime)) {
                TotalDto totalDto = TotalDto.builder().dateTime(startTime).build();
                startTime = startTime.plus(intValue, unit);
                linkedList.add(totalDto);
            }
            return linkedList;
        }

        // 结果集的第一天
        HistogramBucket histogramBucket1 = list.get(0);
        LocalDateTime resultStartTime = TimeUtils.timestamToDatetime(new BigDecimal(histogramBucket1.key() + "").longValue());


        // 结果集的最后一天
        HistogramBucket histogramBucket2 = list.get(list.size() - 1);
        LocalDateTime resultEndTime = TimeUtils.timestamToDatetime(new BigDecimal(histogramBucket2.key() + "").longValue());


        // 填充开始的时间
        while (startTime.until(resultStartTime, unit)>0) {
            TotalDto totalDto = TotalDto.builder().dateTime(startTime).build();
            startTime = startTime.plus(intValue, unit);
            linkedList.add(totalDto);
        }


        // 填充中间
        int index = 0;
        while (resultStartTime.isBefore(resultEndTime) || resultStartTime.isEqual(resultEndTime)) {
            HistogramBucket histogramBucket = list.get(index);
            Map<String, Aggregate> map = histogramBucket.aggregations();
            TotalDto totalDto = TotalDto.builder()
                    .dateTime(resultStartTime)
                    .count(histogramBucket.docCount())
                    .pageCount(map.get("pageCount").cardinality().value())
                    .userCount(map.get("userCount").cardinality().value())
                    .build();
            linkedList.add(totalDto);
            resultStartTime = resultStartTime.plus(intValue, unit);
            index++;
        }

        resultEndTime = resultEndTime.plus(intValue, unit);

        // 填充结束时间
        while (resultEndTime.until(endTime, unit) >0) {
            TotalDto totalDto = TotalDto.builder().dateTime(resultEndTime).build();
            resultEndTime = resultEndTime.plus(intValue, unit);
            linkedList.add(totalDto);
        }

        return linkedList;
    }

}
